package com.roads.workflow.controller;

import com.roads.common.constant.TaskConstants;
import com.roads.common.core.domain.AjaxResult;
import com.roads.common.core.domain.entity.SysDept;
import com.roads.common.core.domain.entity.SysUser;
import com.roads.common.utils.DateUtils;
import com.roads.system.service.ISysUserService;
import com.roads.workflow.domain.WorkflowChart;
import com.roads.workflow.domain.WorkflowMeterageNode;
import com.roads.workflow.domain.WorkflowTask;
import com.roads.workflow.domain.WorkflowTaskStep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.roads.common.core.controller.BaseController;
import com.roads.workflow.service.IWorkflowTaskService;

import java.util.ArrayList;
import java.util.List;

/**
 * 工作流任务Controller
 * 
 * @author nhp
 * @date 2022-01-09
 */
@RestController
@RequestMapping("/workflow/task")
public class WorkflowTaskController extends BaseController {

    @Autowired
    private IWorkflowTaskService service;

    @Autowired
    private ISysUserService userService;


    /**
     * 获取流程节点
     *
     * @param taskUuid 任务uuid
     * @return 结果
     */
    @GetMapping("getWorkflowNode")
    public AjaxResult getWorkflowNode(String taskUuid) {
        List<WorkflowMeterageNode> nodes = service.selectListWorkflowNodeByTaskUuid(taskUuid);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("nodes", nodes);
        return ajaxResult;
    }

    /**
     * 获取流程节点用户
     *
     * @param taskUuid 任务uuid
     * @param workflowNodeUuid 流程节点uuid
     * @return 结果
     */
    @GetMapping("getWorkflowNodeUser")
    public AjaxResult getWorkflowNodeUser(String taskUuid, String workflowNodeUuid) {
        AjaxResult ajaxResult = new AjaxResult();
        List<SysUser> nodeUsers = service.getWorkflowNodeUser(taskUuid, workflowNodeUuid);
        ajaxResult.put("users", nodeUsers);
        return ajaxResult;
    }

    /**
     * 获取流程步骤
     *
     * @param taskUuid 任务uuid
     * @return 结果
     */
    @GetMapping("getWorkflowStep")
    public AjaxResult getWorkflowStep(String taskUuid) {
        List<WorkflowTaskStep> workflowTaskSteps = service.getWorkflowStep(taskUuid);
        List<WorkflowChart> taskSteps = new ArrayList<>();
        // 重新组织内容
        for (WorkflowTaskStep taskStep: workflowTaskSteps) {
            WorkflowChart step = new WorkflowChart();
            String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, taskStep.getCreateTime());
            step.setDate(date);
            step.setType(taskStep.getActionType());
            if (TaskConstants.TASK_ACTION_TYPE_CREATE.equals(taskStep.getActionType())) {
                step.setTitle("创建任务");
                step.setContent(taskStep.getUserName() + "  " + date + "  创建任务");
                step.setColor("#2a5caa");
            } else if (TaskConstants.TASK_ACTION_TYPE_UPLOAD.equals(taskStep.getActionType())){
                step.setTitle("上传数据");
                step.setContent(taskStep.getCreateByName() + "  " + date + "  上传至" + taskStep.getUserName());
                step.setColor("#2a5caa");
            } else if (TaskConstants.TASK_ACTION_TYPE_RETURN.equals(taskStep.getActionType())){
                step.setTitle("回退数据");
                step.setContent(taskStep.getCreateByName() + "  " + date + "  回退至" + taskStep.getUserName());
                step.setColor("#bb505d");
            } else if (TaskConstants.TASK_ACTION_TYPE_FINISH.equals(taskStep.getActionType())){
                step.setTitle("结束任务");
                step.setContent(taskStep.getCreateByName()  + "  " + date + "  结束任务");
                step.setColor("#2a5caa");
            }
            step.setReason(taskStep.getReason());
            taskSteps.add(step);
        }
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("taskSteps", taskSteps);
        return ajaxResult;
    }

}
